(function($){
    var result = new Object();
    result.isInDB = false;
	result.urls = new Object();
    result._validations = new Array();
    /*---------------------------------------------------------------------*/
    result.addValidation = function(validation){
        if (typeof(validation) == "function") {
            result._validations.push(validation);
        }
    };
    /*---------------------------------------------------------------------*/
    result.refreshQuesRootList = function(){
        $("#quesRootList").load(result.urls["quesList"], function(){
            result.bindQuesClick();
        });
    };
    /*---------------------------------------------------------------------*/
    result.bindQuesClick = function(){
        $("#quesRootList a").click(function(){
            $("#quesRootList td").removeClass("highlight");
            var td = $(this).parent();
            td.addClass("highlight");
            $("#arena").empty();
            result.refreshResultList(td.attr("oid"), 1);
        });
    };
    /*---------------------------------------------------------------------*/
    result.refreshResultList = function(id, pn){
        $("#chute").load(result.urls["list"], {
            id: id,
            pn: pn
        }, function(){
            result.bindResultClick($("#chute"));
        });
    };
    /*---------------------------------------------------------------------*/
    result.bindResultClick = function(jq){
        $("div[resultId]>a", jq).click(function(){
            var p = $(this).parent();
            result.refreshResultDetail(p.attr("resultId"), p.attr("quesId"), p.attr("parentId"));
            //set select satus
            $("div[resultId]>a.selected").removeClass("selected");
            if ("0" != p.attr("resultId")) {
                $(this).addClass("selected");
            }
        });
        $("div.pager a", jq).click(function(){
            var a = $(this);
            result.refreshResultList(a.attr("quesId"), a.attr("pn"));
        });
        /* render handle */
        
        $("div.node", jq).children("img").toggle(function(){
            var handle = $(this);
            var div = handle.parent();
            var sub = div.children("div");
            sub.load(result.urls["list"], {
                parentId: div.attr("resultId")
            }, function(){
                handle.attr("src", handle.attr("open"));
                result.bindResultClick(sub);
            });
        }, function(){
            var handle = $(this);
            handle.parent().children("div").empty();
            handle.attr("src", handle.attr("close"));
        });
    };
    /*---------------------------------------------------------------------*/
    result.refreshResultDetail = function(id, quesId, parentId, forbidsub){
        $("#arena").empty();
        $("#arena").load(result.urls["edit"], {
            id: id,
            quesId: quesId,
            parentId: parentId,
            forbidsub: forbidsub ? true : false
        }, function(){
            result.renderResultDetail();
        });
    };
    /*---------------------------------------------------------------------*/
    result.renderResultDetail = function(){
        result.bindNewSubResultClick();
        result.bindResultDeleteClick();
        result.bindResultSaveClick();
		result.bindClearSingleSelect();
    };
	/*---------------------------------------------------------------------*/
	result.bindClearSingleSelect = function(){
		$("#arena *[subjectType=2] div.subjectTitle img").bind("click",function(){
			$(this).parents(".subjectOutline:first").find(":radio").each(function(index,ele){
				ele.checked = false;
			});
			$(this).parents(".subjectOutline:first").find(":text").each(function(index,ele){
				ele.value = "";
			});
		})
	};
    /*---------------------------------------------------------------------*/
    result.bindNewSubResultClick = function(){
        $("#arena a[quesId]").unbind("click");
        $("#arena a[quesId]").click(function(){
            var p = $(this).parents("div.actionBar");
            if ("0" != p.attr("resultId")) {
                result.refreshResultDetail("0", $(this).attr("quesId"), p.attr("resultId"));
            }
        });
    };
    /*---------------------------------------------------------------------*/
    result.bindResultDeleteClick = function(){
        $("#arena a.delete").unbind("click");
        $("#arena a.delete").click(function(){
            var p = $(this).parents("div.actionBar");
            $("#arena").load(result.urls["del"], {
                id: p.attr("resultId")
            }, function(){
                result.refreshResultList($("#chute div.innerMenu").attr("quesId"), 1);
            });
        });
    };
    /*---------------------------------------------------------------------*/
    result.bindResultSaveClick = function(){
        $("#arena a.save").unbind("click");
        $("#arena a.save").click(function(){
            if (!result.validate()) {
                return false;
            }
            var p = $(this).parents("div.actionBar");
            var data = new Object();
            data["id"] = p.attr("resultId");
            data["quesId"] = p.attr("quesId");
            data["parentId"] = p.attr("parentId");
            //fill the data
            $("*[subjectId]").each(function(){
                var divS = $(this);
                var id = divS.attr("subjectId");
                var type = divS.attr("subjectType");
                if ("1" == type) {
                    var value = $(">input,>textarea", this).val();
                    if ("" != value && undefined != value) {
                        data["s." + id + ".str"] = value;
                    }
                }
                else 
                    if ("2" == type) {
                        var options = $("*[isExt]", this);
                        var numV = "";
                        var strV = "";
                        options.each(function(){
                            var radioV = $("input[type=radio]", this);
                            if (radioV.get(0).checked) {
                                numV = radioV.val();
                                var ext = $("input[type=text],>textarea", this);
                                if (ext.length > 0) {
                                    strV = ext.val();
                                }
                            }
                        });
                        data["s." + id + ".str"] = strV;
                        data["s." + id + ".num"] = numV;
                    }
                    else 
                        if ("3" == type) {
                            var options = $("*[isExt]", this);
                            var numV = "";
                            var strV = "";
                            options.each(function(){
                                var checkV = $("input[type=checkbox]", this);
                                if (checkV.get(0).checked) {
                                    numV = numV + checkV.val() + ",";
                                    var ext = $("input[type=text],textarea", this);
                                    if (ext.length > 0) {
                                        strV = strV + ext.val();
                                    }
                                    strV = strV + ",";
                                }
                                else {
                                    numV = numV + ",";
                                    strV = strV + ",";
                                }
                            });
                            data["s." + id + ".str"] = strV;
                            data["s." + id + ".num"] = numV;
                        }
                        else 
                            if ("4" == type) {
                                data["s." + id + ".num"] = $("select", this).val();
                            }
            });
            $.ajax({
                url: result.urls["save"],
                data: data,
                processData: true,
                cache: false,
                async: true,
                type: "POST",
                dataType: "html",
                success: function(html){
                    if (html == "REJECT") {
                        if ($.result.isInDB) {
                            alert("机构请输入机构名称，数据库请输入数据库名，用户请输入用户名称!");
                        }
                        else {
                            alert("机构问卷请输入机构名称，数据库问卷请输入数据库名，用户问卷请输入用户名称!");
                        }
                    }
                    else {
                        $("#arena").html(html);
                        result.refreshResultList($("#chute div.innerMenu").attr("quesId"), 1);
                        result.bindResultClick($("#arena"));
                    }
                }
            });
        });
    };
    /*---------------------------------------------------------------------*/
    result.refresh = function(){
		result.initUrls();
        result.refreshQuesRootList();
    }
	/*---------------------------------------------------------------------*/
	result.initUrls = function(){
		if(result.isInDB){
			result.urls["quesList"] = ROOT + "/logined/result/quesListDB.do";
			result.urls["list"] = ROOT + "/logined/result/list.do?type=1";
			result.urls["edit"] = ROOT + "/logined/result/editDB.do";
			result.urls["save"] = ROOT + "/logined/result/save.do?type=1";
		}else{
			result.urls["quesList"] = ROOT + "/logined/result/quesList.do";
			result.urls["list"] = ROOT + "/logined/result/list.do?type=0";
			result.urls["edit"] = ROOT + "/logined/result/edit.do";
			result.urls["save"] = ROOT + "/logined/result/save.do?type=0";
		}
		result.urls["del"] = ROOT + "/logined/result/del.do";
	};
    /*---------------------------------------------------------------------*/
    result.validate = function(){
        for (var i in result._validations) {
            var validation = result._validations[i];
            if (typeof(validation) == "function") {
                if (!validation()) {
                    return false;
                }
            }
        }
        return true;
    };
    
    // Install to jQuery
    $.result = result;
})(jQuery);
